
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>日期和时间 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="mix-tasks.html" />
    
    
    <link rel="prev" href="strings.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">第一部分：基础</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="basics.html">
            
                <a href="basics.html">
            
                    
                    基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="collections.html">
            
                <a href="collections.html">
            
                    
                    集合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="enum.html">
            
                <a href="enum.html">
            
                    
                    Enum 模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="pattern-matching.html">
            
                <a href="pattern-matching.html">
            
                    
                    模式匹配
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="control-structures.html">
            
                <a href="control-structures.html">
            
                    
                    控制语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="functions.html">
            
                <a href="functions.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="pipe-operator.html">
            
                <a href="pipe-operator.html">
            
                    
                    管道操作符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="modules.html">
            
                <a href="modules.html">
            
                    
                    模块(Module)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="mix.html">
            
                <a href="mix.html">
            
                    
                    Mix
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="sigils.html">
            
                <a href="sigils.html">
            
                    
                    魔符(Sigil)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="documentation.html">
            
                <a href="documentation.html">
            
                    
                    文档模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="testing.html">
            
                <a href="testing.html">
            
                    
                    测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="comprehensions.html">
            
                <a href="comprehensions.html">
            
                    
                    推导
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="strings.html">
            
                <a href="strings.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.16" data-path="date-time.html">
            
                <a href="date-time.html">
            
                    
                    日期和时间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="mix-tasks.html">
            
                <a href="mix-tasks.html">
            
                    
                    自定义Mix任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="iex-helpers.html">
            
                <a href="iex-helpers.html">
            
                    
                    IEx辅助函数
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第二部分：高级</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../advanced/erlang.html">
            
                <a href="../advanced/erlang.html">
            
                    
                    和Erlang互操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="../advanced/error-handling.html">
            
                <a href="../advanced/error-handling.html">
            
                    
                    错误处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="../advanced/escripts.html">
            
                <a href="../advanced/escripts.html">
            
                    
                    可执行文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="../advanced/concurrency.html">
            
                <a href="../advanced/concurrency.html">
            
                    
                    并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="../advanced/otp-concurrency.html">
            
                <a href="../advanced/otp-concurrency.html">
            
                    
                    OTP并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="../advanced/otp-supervisors.html">
            
                <a href="../advanced/otp-supervisors.html">
            
                    
                    OTP Supervisors
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="../advanced/otp-distribution.html">
            
                <a href="../advanced/otp-distribution.html">
            
                    
                    OTP 分布式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="../advanced/metaprogramming.html">
            
                <a href="../advanced/metaprogramming.html">
            
                    
                    元编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.9" data-path="../advanced/umbrella-projects.html">
            
                <a href="../advanced/umbrella-projects.html">
            
                    
                    Umbrella Projects
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.10" data-path="../advanced/typespec.html">
            
                <a href="../advanced/typespec.html">
            
                    
                    Specifications and types
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.11" data-path="../advanced/behaviours.html">
            
                <a href="../advanced/behaviours.html">
            
                    
                    行为
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.12" data-path="../advanced/gen-stage.html">
            
                <a href="../advanced/gen-stage.html">
            
                    
                    GenStage
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.13" data-path="../advanced/protocols.html">
            
                <a href="../advanced/protocols.html">
            
                    
                    协议
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.14" data-path="../advanced/nerves.html">
            
                <a href="../advanced/nerves.html">
            
                    
                    Nerves
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第三部分：ECTO</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../ecto/basics.html">
            
                <a href="../ecto/basics.html">
            
                    
                    Basics
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="../ecto/changesets.html">
            
                <a href="../ecto/changesets.html">
            
                    
                    Changesets
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="../ecto/associations.html">
            
                <a href="../ecto/associations.html">
            
                    
                    关联关系
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="../ecto/querying.html">
            
                <a href="../ecto/querying.html">
            
                    
                    查询
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第四部分：专题</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../specifics/plug.html">
            
                <a href="../specifics/plug.html">
            
                    
                    Plug
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="../specifics/eex.html">
            
                <a href="../specifics/eex.html">
            
                    
                    嵌入的 Elixir(EEx)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="../specifics/ets.html">
            
                <a href="../specifics/ets.html">
            
                    
                    Erlang 项式存储(ETS)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="../specifics/mnesia.html">
            
                <a href="../specifics/mnesia.html">
            
                    
                    Mnesia 数据库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="../specifics/debugging.html">
            
                <a href="../specifics/debugging.html">
            
                    
                    调试
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第五部分：程序库</li>
        
        
    
        <li class="chapter " data-level="5.1" data-path="../libraries/guardian.html">
            
                <a href="../libraries/guardian.html">
            
                    
                    Guardian(基础)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="../libraries/poolboy.html">
            
                <a href="../libraries/poolboy.html">
            
                    
                    Poolboy
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="../libraries/benchee.html">
            
                <a href="../libraries/benchee.html">
            
                    
                    Benchee
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="../libraries/bypass.html">
            
                <a href="../libraries/bypass.html">
            
                    
                    Bypass
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="../libraries/distillery.html">
            
                <a href="../libraries/distillery.html">
            
                    
                    Distillery(基础)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="../libraries/stream-data.html">
            
                <a href="../libraries/stream-data.html">
            
                    
                    StreamData
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >日期和时间</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <p>Elixir &#x4E2D;&#x6709;&#x5173;&#x65F6;&#x95F4;&#x548C;&#x65E5;&#x671F;&#x7684;&#x5904;&#x7406;</p>
<h2 id="time">Time</h2>
<p>Elixir &#x5185;&#x7F6E;&#x4E86;&#x51E0;&#x4E2A;&#x5904;&#x7406;&#x65F6;&#x95F4;&#x7684;&#x6A21;&#x5757;&#x3002;&#x4E0D;&#x8FC7;&#x9700;&#x8981;&#x6CE8;&#x610F;&#x7684;&#x662F;&#xFF0C;&#x8FD9;&#x4E9B;&#x6A21;&#x5757;&#x53EA;&#x80FD;&#x5904;&#x7406; UTC &#x65F6;&#x95F4;&#x3002;</p>
<p>&#x9996;&#x5148;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x83B7;&#x53D6;&#x5F53;&#x524D;&#x7684; UTC &#x65F6;&#x95F4;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; Time.utc_now
~T[<span class="hljs-number">19</span><span class="hljs-symbol">:</span><span class="hljs-number">39</span><span class="hljs-symbol">:</span><span class="hljs-number">31.056226</span>]
</code></pre>
<p>&#x6211;&#x4EEC;&#x4E5F;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x9B54;&#x7B26; <code>~T</code> &#x6765;&#x521B;&#x5EFA;&#x65F6;&#x95F4; <code>Time</code> &#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; ~T[<span class="hljs-number">19</span><span class="hljs-symbol">:</span><span class="hljs-number">39</span><span class="hljs-symbol">:</span><span class="hljs-number">31.056226</span>]
~T[<span class="hljs-number">19</span><span class="hljs-symbol">:</span><span class="hljs-number">39</span><span class="hljs-symbol">:</span><span class="hljs-number">31.056226</span>]
</code></pre>
<p><a href="../sigils">&#x9B54;&#x7B26;</a> &#x4E00;&#x8BFE;&#x4E2D;&#x6709;&#x66F4;&#x591A;&#x5173;&#x4E8E;&#x9B54;&#x7B26;&#x7684;&#x4ECB;&#x7ECD;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x5C55;&#x793A;&#x4E86;&#x4ECE;&#x4E00;&#x4E2A; <code>Time</code> &#x7ED3;&#x6784;&#x4F53;&#x4E2D;&#x83B7;&#x53D6;&#x65F6;&#x5206;&#x79D2;&#x7B49;&#x90E8;&#x5206;&#x7684;&#x65B9;&#x6CD5;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; t = ~T[<span class="hljs-number">19</span><span class="hljs-symbol">:</span><span class="hljs-number">39</span><span class="hljs-symbol">:</span><span class="hljs-number">31.056226</span>]
~T[<span class="hljs-number">19</span><span class="hljs-symbol">:</span><span class="hljs-number">39</span><span class="hljs-symbol">:</span><span class="hljs-number">31.056226</span>]
iex&gt; t.hour
<span class="hljs-number">19</span>
iex&gt; t.minute
<span class="hljs-number">39</span>
iex&gt; t.day
** (KeyError) key <span class="hljs-symbol">:day</span> <span class="hljs-keyword">not</span> found <span class="hljs-symbol">in:</span> ~T[<span class="hljs-number">19</span><span class="hljs-symbol">:</span><span class="hljs-number">39</span><span class="hljs-symbol">:</span><span class="hljs-number">31.056226</span>]
</code></pre>
<p>&#x4F60;&#x4E5F;&#x8BB8;&#x5DF2;&#x7ECF;&#x6CE8;&#x610F;&#x5230;&#x4E86;&#xFF0C;<code>Time</code> &#x7ED3;&#x6784;&#x4E2D;&#x53EA;&#x6709;&#x5728;&#x4E00;&#x5929;&#x5185;&#x7684;&#x65F6;&#x95F4;&#x4FE1;&#x606F;&#xFF0C;&#x6CA1;&#x6709;&#x5177;&#x4F53;&#x7684;&#x67D0;&#x5929;&#xFF0C;&#x67D0;&#x6708;&#xFF0C;&#x67D0;&#x65E5;&#x7684;&#x4FE1;&#x606F;&#x3002;</p>
<h2 id="date">Date</h2>
<p>&#x4E0E; <code>Time</code> &#x76F8;&#x5BF9;&#x7684;&#x662F; <code>Date</code> &#x7ED3;&#x6784;&#x4F53;&#xFF0C;&#x5B83;&#x53EA;&#x6709;&#x65E5;&#x671F;&#x7684;&#x4FE1;&#x606F;&#xFF0C;&#x800C;&#x6CA1;&#x6709;&#x65F6;&#x95F4;&#x7684;&#x4FE1;&#x606F;&#x3002;</p>
<pre><code class="lang-elixir">iex&gt; Date.utc_today
~D[<span class="hljs-number">2028</span>-<span class="hljs-number">10</span>-<span class="hljs-number">21</span>]
</code></pre>
<p><code>Date</code> &#x6A21;&#x5757;&#x6709;&#x4E00;&#x4E9B;&#x5B9E;&#x7528;&#x7684;&#x65E5;&#x671F;&#x5904;&#x7406;&#x51FD;&#x6570;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; {<span class="hljs-symbol">:ok</span>, date} = Date.new(<span class="hljs-number">2020</span>, <span class="hljs-number">12</span>, <span class="hljs-number">12</span>)
{<span class="hljs-symbol">:ok</span>, ~D[<span class="hljs-number">2020</span>-<span class="hljs-number">12</span>-<span class="hljs-number">12</span>]}
iex&gt; Date.day_of_week date
<span class="hljs-number">6</span>
iex&gt; Date.leap_year? date
<span class="hljs-keyword">true</span>
</code></pre>
<p><code>day_of_week/1</code> &#x8BA1;&#x7B97;&#x67D0;&#x5929;&#x662F;&#x4E00;&#x4E2A;&#x661F;&#x671F;&#x4E2D;&#x7684;&#x7B2C;&#x51E0;&#x5929;&#x3002;&#x4E0A;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x4E2D;&#x7684;&#x65E5;&#x671F;&#x662F;&#x661F;&#x671F;&#x516D;&#x3002; <code>leap_year?/1</code> &#x68C0;&#x67E5;&#x67D0;&#x5929;&#x7684;&#x5E74;&#x4EFD;&#x662F;&#x4E0D;&#x662F;&#x4E00;&#x4E2A;&#x95F0;&#x5E74;&#x3002;&#x66F4;&#x591A;&#x6709;&#x5173;&#x65E5;&#x671F;&#x7684;&#x51FD;&#x6570;&#x8BF7;&#x53C2;&#x8003;<a href="https://hexdocs.pm/elixir/Date.html" target="_blank">&#x6587;&#x6863;</a>&#x3002;</p>
<h2 id="naivedatetime">NaiveDateTime</h2>
<p>Elixir &#x4E2D;&#x6709;&#x4E24;&#x4E2D;&#x7ED3;&#x6784;&#x4F53;&#x65E2;&#x5305;&#x542B;&#x65F6;&#x95F4;&#x4FE1;&#x606F;&#xFF0C;&#x53C8;&#x5305;&#x542B;&#x65E5;&#x671F;&#x4FE1;&#x606F;&#x3002;&#x6211;&#x4EEC;&#x9996;&#x5148;&#x6765;&#x770B; <code>NaiveDateTime</code>&#x3002; &#x5B83;&#x7684;&#x4E00;&#x4E2A;&#x4E0D;&#x8DB3;&#x662F;&#x7F3A;&#x5C11;&#x65F6;&#x533A;&#x7684;&#x4FE1;&#x606F;&#x3002;</p>
<pre><code class="lang-elixir">iex(<span class="hljs-number">15</span>)&gt; NaiveDateTime.utc_now
~N[<span class="hljs-number">2029</span>-<span class="hljs-number">01</span>-<span class="hljs-number">21</span> <span class="hljs-number">19</span><span class="hljs-symbol">:</span><span class="hljs-number">55</span><span class="hljs-symbol">:</span><span class="hljs-number">10.008965</span>]
</code></pre>
<p>&#x4F46;&#x662F;&#x56E0;&#x4E3A;&#x5B83;&#x540C;&#x65F6;&#x6709;&#x65F6;&#x95F4;&#x548C;&#x65E5;&#x671F;&#xFF0C;&#x6240;&#x4EE5;&#x4F60;&#x53EF;&#x4EE5;&#x5BF9;&#x5B83;&#x8FDB;&#x884C;&#x65F6;&#x95F4;&#x7684;&#x8FD0;&#x7B97;&#xFF0C;&#x4E0B;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x5C55;&#x793A;&#x4E86;&#x5BF9;&#x4E00;&#x4E2A; <code>NaiveDateTime</code> &#x7ED3;&#x6784;&#x52A0; 30 &#x79D2;&#x7684;&#x64CD;&#x4F5C;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; NaiveDateTime.add(~N[<span class="hljs-number">2018</span>-<span class="hljs-number">10</span>-<span class="hljs-number">01</span> <span class="hljs-number">00</span><span class="hljs-symbol">:</span><span class="hljs-number">00</span><span class="hljs-symbol">:</span><span class="hljs-number">14</span>], <span class="hljs-number">30</span>)
~N[<span class="hljs-number">2018</span>-<span class="hljs-number">10</span>-<span class="hljs-number">01</span> <span class="hljs-number">00</span><span class="hljs-symbol">:</span><span class="hljs-number">00</span><span class="hljs-symbol">:</span><span class="hljs-number">44</span>]
</code></pre>
<h2 id="datetime">DateTime</h2>
<p>&#x4F60;&#x53EF;&#x80FD;&#x5DF2;&#x7ECF;&#x731C;&#x5230;&#x4E86;&#xFF0C;Elixir &#x5185;&#x7F6E;&#x7684;&#x7B2C;&#x4E8C;&#x4E2A;&#x65E2;&#x6709;&#x65F6;&#x95F4;&#xFF0C;&#x53C8;&#x6709;&#x65E5;&#x671F;&#x7684;&#x6A21;&#x5757;&#x662F; <code>DateTime</code>&#xFF0C;&#x800C;&#x4E14;&#x5B83;&#x8FD8;&#x6709;&#x65F6;&#x533A;&#x7684;&#x4FE1;&#x606F;&#x3002;&#x9700;&#x8981;&#x6CE8;&#x610F;&#x7684;&#x662F;&#xFF0C;&#x6709;&#x5173;&#x65F6;&#x533A;&#xFF0C;&#x5B98;&#x65B9;&#x6587;&#x6863;&#x505A;&#x7684;&#x7279;&#x522B;&#x7684;&#x8BF4;&#x660E;&#xFF1A;</p>
<pre><code>&#x672C;&#x6A21;&#x5757;&#x53EA;&#x5305;&#x62EC;&#x4E86;&#x4E00;&#x4E9B;&#x8F6C;&#x6362;&#x51FD;&#x6570;&#x548C;&#x5904;&#x7406; UTC &#x7684;&#x51FD;&#x6570;&#x3002;&#x8FD9;&#x662F;&#x56E0;&#x4E3A;&#x8981;&#x5B8C;&#x6574;&#x5B9E;&#x73B0; DateTime &#x9700;&#x8981;&#x4E00;&#x4E2A;&#x65F6;&#x533A;&#x6570;&#x636E;&#x5E93;&#xFF0C;&#x4F46;&#x662F;&#x76EE;&#x524D; Elixir &#x8FD8;&#x672A;&#x63D0;&#x4F9B;&#x6B64;&#x6570;&#x636E;&#x5E93;&#x3002;
</code></pre><p>&#x539F;&#x6587;&#x662F;&#xFF1A;</p>
<pre><code>You will notice this module only contains conversion functions as well as functions that work on UTC. This is because a proper DateTime implementation requires a time zone database which currently is not provided as part of Elixir.
</code></pre><p>&#x53E6;&#x5916;&#xFF0C;&#x4F60;&#x4E5F;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x4E00;&#x4E2A; <code>NaiveDateTime</code> &#x7684;&#x503C;&#x6765;&#x521B;&#x5EFA;&#x4E00;&#x4E2A; <code>DateTime</code>&#xFF0C;&#x53EA;&#x9700;&#x8981;&#x901A;&#x8FC7; <code>from_naive/2</code> &#x52A0;&#x4E0A;&#x65F6;&#x533A;&#x4FE1;&#x606F;&#x5373;&#x53EF;&#xFF1A;</p>
<pre><code>iex&gt; DateTime.from_naive(~N[2016-05-24 13:26:08.003], &quot;Etc/UTC&quot;)
{:ok, #DateTime&lt;2016-05-24 13:26:08.003Z&gt;}
</code></pre><p>&#x5982;&#x679C;&#x4F60;&#x60F3;&#x4E86;&#x89E3;&#x66F4;&#x591A;&#x6709;&#x5173;&#x65F6;&#x95F4;&#xFF0C;&#x65E5;&#x671F;&#x7684;&#x66F4;&#x9AD8;&#x7EA7;&#x7684;&#x7528;&#x6CD5;&#xFF0C;&#x8BF7;&#x53C2;&#x8003;&#x8FD9;&#x4E9B;&#x6587;&#x6863;&#xFF1A; <a href="https://hexdocs.pm/elixir/Time.html" target="_blank">Time</a>, <a href="https://hexdocs.pm/elixir/Date.html" target="_blank">Date</a>, <a href="https://hexdocs.pm/elixir/DateTime.html" target="_blank">DateTime</a>&#x3002;&#x4F60;&#x4E5F;&#x53EF;&#x4EE5;&#x8003;&#x8651; <a href="https://github.com/bitwalker/timex" target="_blank">Timex</a> &#x548C; <a href="https://github.com/lau/calendar" target="_blank">Calendar</a> &#x3002;&#x5B83;&#x4EEC;&#x90FD;&#x662F;&#x5F88;&#x5F3A;&#x5927;&#x7684;&#x5904;&#x7406;&#x65F6;&#x95F4;&#x7684;&#x5E93;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="strings.html" class="navigation navigation-prev " aria-label="Previous page: 字符串">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="mix-tasks.html" class="navigation navigation-next " aria-label="Next page: 自定义Mix任务">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"version":"1.0.1","title":"日期和时间","level":"1.16","depth":1,"next":{"title":"自定义Mix任务","level":"1.17","depth":1,"path":"lessons/basics/mix-tasks.md","ref":"lessons/basics/mix-tasks.md","articles":[]},"previous":{"title":"字符串","level":"1.15","depth":1,"path":"lessons/basics/strings.md","ref":"lessons/basics/strings.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"lessons/basics/date-time.md","mtime":"2019-11-05T02:25:51.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-11-05T03:01:22.004Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

